
#include "propselector.h"
#include "gui_utils.h"
#include "taskinfo.h"


#include <iostream>
using namespace std;

PropSelector::PropSelector(TreeView *view, QWidget *parent)
    : QMenu(parent),
      m_view(view)
{
    int prop_count = TIPUtils::getPropCount();
    for (int i = 0; i < prop_count; ++i) {
        TIP prop = TIPUtils::getPropAt(i);

        QAction *action = this->addAction(GUIUtils::propToString(prop));
        m_actions.push_back(action);
        action->setCheckable(true);
        action->setChecked(view->isPropShown(prop));

        ToggledReceiver *receiver = new ToggledReceiver(view, prop);
        connect(action, &QAction::toggled, [receiver](bool checked){
            receiver->receive(checked);
        });

    }

    connect(view, SIGNAL(onPropShownToggled(TIP,bool)), this, SLOT(_after_prop_shown_toggled(TIP,bool)));
}

void PropSelector::_after_prop_shown_toggled(TIP prop, bool shown)
{
    QAction * p = m_actions[TIPUtils::getPropIndex(prop)];
    p->setChecked(shown);
}

ToggledReceiver::ToggledReceiver(TreeView *view, TIP prop)
    : m_view(view),
      m_prop(prop) {}

void ToggledReceiver::receive(bool checked)
{
    m_view->setPropShown(m_prop, checked);
}
